package timing.ukulele.auth.util;

import cn.hutool.captcha.ShearCaptcha;
import timing.ukulele.auth.support.RedisOperator;
import timing.ukulele.auth.vo.CaptchaVO;
import timing.ukulele.common.data.ResponseData;

import java.util.UUID;

import static timing.ukulele.auth.constant.RedisConstants.DEFAULT_TIMEOUT_SECONDS;

public class CommonUtil {

    /**
     * 使用huTool-captcha生成图形验证码
     */
    public static ResponseData<CaptchaVO> createCaptcha(RedisOperator<String> stringRedisOperator, String prefix) {
        // 定义图形验证码的长、宽、验证码字符数、干扰线宽度
        ShearCaptcha captcha = cn.hutool.captcha.CaptchaUtil.createShearCaptcha(130, 34, 4, 2);
        // 生成一个唯一id
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        // 存入缓存中，5分钟后过期
        stringRedisOperator.set(prefix + id, captcha.getCode(), DEFAULT_TIMEOUT_SECONDS);
        return ResponseData.success("获取验证码成功.", new CaptchaVO(id, null, captcha.getImageBase64Data()));
    }
}
